package com.fr.cell.core;

import com.fr.base.BaseUtils;
import com.fr.base.ColumnRow;
import com.fr.base.FRContext;
import com.fr.cell.GridSelection;
import com.fr.cell.ReportPane;
import com.fr.cell.clipboard.CellElementsClip;
import com.fr.cell.clipboard.ElementsTransferable;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.Report;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.core.DSColumn;
import java.awt.Rectangle;
import java.io.PrintStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class IntelliElements
{
  public static final int DIRECTION_UNDEF = -1;
  public static final int DIRECTION_UP_TO_DOWN = 0;
  public static final int DIRECTION_DOWN_TO_UP = 1;
  public static final int DIRECTION_LEFT_TO_RIGHT = 2;
  public static final int DIRECTION_RIGHT_TO_LEFT = 3;
  public static final int ACTION_SEQUENCING = 0;
  public static final int ACTION_REPLICATION = 1;
  public static final int FORMULA_NONE_PARA_SAME = -1;
  public static final int FORMULA_HOR_PARA_SAME = 0;
  public static final int FORMULA_VER_PARA_SAME = 1;
  private int direction = -1;
  private int action = 0;
  private boolean isStyleSupported = true;
  private ReportPane reportPane;
  private Report report;
  private Rectangle oldCellRectangle = null;
  private Rectangle editRectangle;
  private Rectangle dragCellRectangle = null;
  private boolean isNeedRepaint;
  private boolean isDataChanged;

  public static void iterating(ReportPane paramReportPane, Rectangle paramRectangle1, Rectangle paramRectangle2, Rectangle paramRectangle3, boolean paramBoolean1, boolean paramBoolean2)
  {
    IntelliElements localIntelliElements = new IntelliElements(paramReportPane, paramRectangle1, paramRectangle2, paramRectangle3, paramBoolean1, paramBoolean2);
    localIntelliElements.setAction(0);
    localIntelliElements.setStyleSupported(true);
    localIntelliElements.doIntelliAction();
  }

  public IntelliElements(ReportPane paramReportPane, Rectangle paramRectangle1, Rectangle paramRectangle2, Rectangle paramRectangle3, boolean paramBoolean1, boolean paramBoolean2)
  {
    this.reportPane = paramReportPane;
    this.report = paramReportPane.getEditingReport();
    this.oldCellRectangle = paramRectangle2;
    this.editRectangle = paramRectangle1;
    this.dragCellRectangle = paramRectangle3;
    this.isNeedRepaint = paramBoolean1;
    this.isDataChanged = paramBoolean2;
  }

  public void setAction(int paramInt)
  {
    this.action = paramInt;
  }

  public void setStyleSupported(boolean paramBoolean)
  {
    this.isStyleSupported = paramBoolean;
  }

  public void doIntelliAction()
  {
    int i;
    GridSelection localGridSelection2;
    int j;
    int k;
    int l;
    int i1;
    int i2;
    Object localObject1;
    DefaultCellElement localDefaultCellElement2;
    analyzeDirection();
    defineRules();
    if (this.action == 1)
    {
      doReplication();
      return;
    }
    if (this.direction == 0)
    {
      i = this.oldCellRectangle.x;
      while (true)
      {
        if (i >= this.oldCellRectangle.x + this.oldCellRectangle.width)
          break label1722;
        localGridSelection2 = this.reportPane.getGridSelection();
        localGridSelection2.getCellRectangle(0);
        j = 0;
        k = localGridSelection2.getCellRectangleCount();
        for (l = 0; l < k; ++l)
          j = localGridSelection2.getCellRectangle(l).height;
        if (k > 1)
          return;
        l = this.oldCellRectangle.y + j;
        i1 = this.dragCellRectangle.y + this.dragCellRectangle.height;
        for (i2 = l; i2 < i1; ++i2)
        {
          Object localObject2;
          localObject1 = this.report.getCellElement(i, i2 - this.oldCellRectangle.height);
          if (localObject1 == null)
            localObject1 = new DefaultCellElement();
          if ((((CellElement)localObject1).getColumnSpan() > 1) || (((CellElement)localObject1).getRowSpan() > 1))
            System.out.println("column = " + i + ", row = " + i2);
          if (((CellElement)localObject1).getValue() instanceof DSColumn)
          {
            localObject2 = (DSColumn)((CellElement)localObject1).getValue();
            DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(i, i2);
            localDefaultCellElement1.setValue(localObject2);
            applyStyle(localDefaultCellElement1, (CellElement)localObject1);
            localDefaultCellElement1.setCellExpandAttr(((CellElement)localObject1).getCellExpandAttr());
            this.report.addCellElement(localDefaultCellElement1);
          }
          else if (((CellElement)localObject1).getValue() instanceof Integer)
          {
            localObject2 = (Integer)((CellElement)localObject1).getValue();
            int i3 = ((Integer)localObject2).intValue() + 1;
            localObject2 = new Integer(i3);
            DefaultCellElement localDefaultCellElement3 = new DefaultCellElement(i, i2);
            localDefaultCellElement3.setValue(localObject2);
            applyStyle(localDefaultCellElement3, (CellElement)localObject1);
            this.report.addCellElement(localDefaultCellElement3);
          }
          else if (((CellElement)localObject1).getValue() instanceof Double)
          {
            localObject2 = (Double)((CellElement)localObject1).getValue();
            double d = ((Double)localObject2).doubleValue() + 1.0D;
            localObject2 = new Double(d);
            DefaultCellElement localDefaultCellElement4 = new DefaultCellElement(i, i2);
            localDefaultCellElement4.setValue(localObject2);
            applyStyle(localDefaultCellElement4, (CellElement)localObject1);
            this.report.addCellElement(localDefaultCellElement4);
          }
          else if (((CellElement)localObject1).getValue() instanceof Formula)
          {
            localObject2 = (Formula)((CellElement)localObject1).getValue();
            localObject2 = generateSimpleFormula((Formula)localObject2, 1, 0);
            localDefaultCellElement2 = new DefaultCellElement(i, i2);
            localDefaultCellElement2.setValue(localObject2);
            applyStyle(localDefaultCellElement2, (CellElement)localObject1);
            this.report.addCellElement(localDefaultCellElement2);
          }
          else
          {
            try
            {
              localObject2 = (CellElement)((CellElement)localObject1).clone();
              localDefaultCellElement2 = new DefaultCellElement(i, i2);
              localDefaultCellElement2.setValue(((CellElement)localObject2).getValue());
              this.report.addCellElement(localDefaultCellElement2);
            }
            catch (CloneNotSupportedException localCloneNotSupportedException1)
            {
              FRContext.getLogger().log(Level.INFO, localCloneNotSupportedException1.getMessage(), localCloneNotSupportedException1);
            }
          }
        }
        ++i;
      }
    }
    if (this.direction == 1)
    {
      i = this.oldCellRectangle.x;
      while (true)
      {
        if (i >= this.oldCellRectangle.x + this.oldCellRectangle.width)
          break label1722;
        ++i;
      }
    }
    if (this.direction == 2)
    {
      i = this.oldCellRectangle.y;
      while (true)
      {
        if (i >= this.oldCellRectangle.y + this.oldCellRectangle.height)
          break label1722;
        localGridSelection2 = this.reportPane.getGridSelection();
        localGridSelection2.getCellRectangle(0);
        j = 0;
        k = localGridSelection2.getCellRectangleCount();
        for (l = 0; l < k; ++l)
          j = localGridSelection2.getCellRectangle(l).width;
        if (k > 1)
          return;
        l = this.oldCellRectangle.x + j;
        i1 = this.dragCellRectangle.x + this.dragCellRectangle.width;
        for (i2 = l; i2 < i1; ++i2)
        {
          Object localObject3;
          localObject1 = this.report.getCellElement(i2 - this.oldCellRectangle.width, i);
          if (localObject1 == null)
            localObject1 = new DefaultCellElement();
          if (((CellElement)localObject1).getValue() instanceof DSColumn)
          {
            localObject3 = (DSColumn)((CellElement)localObject1).getValue();
            localDefaultCellElement2 = new DefaultCellElement(i2, i);
            localDefaultCellElement2.setValue(localObject3);
            applyStyle(localDefaultCellElement2, (CellElement)localObject1);
            localDefaultCellElement2.setCellExpandAttr(((CellElement)localObject1).getCellExpandAttr());
            this.report.addCellElement(localDefaultCellElement2);
          }
          else if (((CellElement)localObject1).getValue() instanceof Integer)
          {
            localObject3 = (Integer)((CellElement)localObject1).getValue();
            localDefaultCellElement2 = new DefaultCellElement(i2, i);
            localDefaultCellElement2.setValue(localObject3);
            applyStyle(localDefaultCellElement2, (CellElement)localObject1);
            this.report.addCellElement(localDefaultCellElement2);
          }
          else if (((CellElement)localObject1).getValue() instanceof Double)
          {
            localObject3 = (Double)((CellElement)localObject1).getValue();
            localDefaultCellElement2 = new DefaultCellElement(i2, i);
            localDefaultCellElement2.setValue(localObject3);
            applyStyle(localDefaultCellElement2, (CellElement)localObject1);
            this.report.addCellElement(localDefaultCellElement2);
          }
          else if (((CellElement)localObject1).getValue() instanceof Formula)
          {
            localObject3 = (Formula)((CellElement)localObject1).getValue();
            localObject3 = generateSimpleFormula((Formula)localObject3, 1, 2);
            localDefaultCellElement2 = new DefaultCellElement(i2, i);
            localDefaultCellElement2.setValue(localObject3);
            applyStyle(localDefaultCellElement2, (CellElement)localObject1);
            this.report.addCellElement(localDefaultCellElement2);
          }
          else
          {
            try
            {
              localObject3 = (CellElement)((CellElement)localObject1).clone();
              localDefaultCellElement2 = new DefaultCellElement(i2, i);
              localDefaultCellElement2.setValue(((CellElement)localObject3).getValue());
              this.report.addCellElement(localDefaultCellElement2);
            }
            catch (CloneNotSupportedException localCloneNotSupportedException2)
            {
              localCloneNotSupportedException2.printStackTrace();
            }
          }
        }
        ++i;
      }
    }
    if (this.direction == 3)
      for (i = this.oldCellRectangle.y; i < this.oldCellRectangle.y + this.oldCellRectangle.height; ++i)
      {
        localGridSelection2 = this.reportPane.getGridSelection();
        localGridSelection2.getCellRectangle(0);
        j = localGridSelection2.getCellRectangleCount();
        if (j > 1)
          return;
        k = this.dragCellRectangle.x;
        l = this.oldCellRectangle.x;
        for (i1 = k; i1 < l; ++i1)
        {
          Object localObject4;
          i2 = i1 - k;
          localObject1 = this.report.getCellElement(l + i2 % this.oldCellRectangle.width, i);
          if (localObject1 == null)
            localObject1 = new DefaultCellElement();
          if (((CellElement)localObject1).getValue() instanceof DSColumn)
          {
            localObject4 = (DSColumn)((CellElement)localObject1).getValue();
            localDefaultCellElement2 = new DefaultCellElement(i1, i);
            localDefaultCellElement2.setValue(localObject4);
            applyStyle(localDefaultCellElement2, (CellElement)localObject1);
            localDefaultCellElement2.setCellExpandAttr(((CellElement)localObject1).getCellExpandAttr());
            this.report.addCellElement(localDefaultCellElement2);
          }
          else if (((CellElement)localObject1).getValue() instanceof Integer)
          {
            localObject4 = (Integer)((CellElement)localObject1).getValue();
            localDefaultCellElement2 = new DefaultCellElement(i1, i);
            localDefaultCellElement2.setValue(localObject4);
            applyStyle(localDefaultCellElement2, (CellElement)localObject1);
            this.report.addCellElement(localDefaultCellElement2);
          }
          else if (((CellElement)localObject1).getValue() instanceof Double)
          {
            localObject4 = (Double)((CellElement)localObject1).getValue();
            localDefaultCellElement2 = new DefaultCellElement(i1, i);
            localDefaultCellElement2.setValue(localObject4);
            applyStyle(localDefaultCellElement2, (CellElement)localObject1);
            this.report.addCellElement(localDefaultCellElement2);
          }
          else if (((CellElement)localObject1).getValue() instanceof Formula)
          {
            localObject4 = (Formula)((CellElement)localObject1).getValue();
            localDefaultCellElement2 = new DefaultCellElement(i1, i);
            localDefaultCellElement2.setValue(localObject4);
            applyStyle(localDefaultCellElement2, (CellElement)localObject1);
            this.report.addCellElement(localDefaultCellElement2);
          }
          else
          {
            try
            {
              localObject4 = (CellElement)((CellElement)localObject1).clone();
              localObject4 = ((CellElement)localObject4).deriveCellElement(i1, i);
              this.report.addCellElement((CellElement)localObject4);
            }
            catch (CloneNotSupportedException localCloneNotSupportedException3)
            {
              FRContext.getLogger().log(Level.INFO, localCloneNotSupportedException3.getMessage(), localCloneNotSupportedException3);
            }
          }
        }
      }
    label1722: GridSelection localGridSelection1 = this.reportPane.getGridSelection();
    localGridSelection1.setType(0);
    localGridSelection1.setEditBounds(this.editRectangle.x, this.editRectangle.y, this.editRectangle.width, this.editRectangle.height);
    localGridSelection1.setEditBounds(this.dragCellRectangle.x, this.dragCellRectangle.y, this.dragCellRectangle.width, this.dragCellRectangle.height);
    this.isNeedRepaint = true;
    this.isDataChanged = true;
    this.reportPane.fireGridSelectionChanged();
    if (this.isDataChanged)
      this.reportPane.repaint();
  }

  private void doReplication()
  {
    ElementsTransferable localElementsTransferable;
    CellElementsClip localCellElementsClip;
    Object localObject;
    if (this.direction == 0)
    {
      if (this.dragCellRectangle.height == this.oldCellRectangle.height)
      {
        this.isNeedRepaint = true;
      }
      else if (this.dragCellRectangle.height > this.oldCellRectangle.height)
      {
        localElementsTransferable = GridUtils.caculateElementsTransferable(this.reportPane);
        localCellElementsClip = null;
        localObject = localElementsTransferable.getFirstObject();
        if ((localObject != null) && (localObject instanceof CellElementsClip))
          localCellElementsClip = (CellElementsClip)localObject;
        if (localCellElementsClip != null)
        {
          int i = this.oldCellRectangle.y + this.oldCellRectangle.height;
          while (i < this.dragCellRectangle.y + this.dragCellRectangle.height)
          {
            pasteElementsCopyToReport(localCellElementsClip, this.reportPane.getEditingReport(), this.oldCellRectangle.x, i, this.oldCellRectangle.x, i, this.oldCellRectangle.width, Math.min(this.oldCellRectangle.height, this.dragCellRectangle.y + this.dragCellRectangle.height - i));
            i += this.oldCellRectangle.height;
          }
        }
        GridSelection localGridSelection1 = this.reportPane.getGridSelection();
        localGridSelection1.setType(0);
        localGridSelection1.setEditBounds(this.editRectangle.x, this.editRectangle.y, this.editRectangle.width, this.editRectangle.height);
        localGridSelection1.setEditBounds(this.dragCellRectangle.x, this.dragCellRectangle.y, this.dragCellRectangle.width, this.dragCellRectangle.height);
        this.isDataChanged = true;
      }
    }
    else if (this.direction == 1)
    {
      localElementsTransferable = GridUtils.caculateElementsTransferable(this.reportPane);
      localCellElementsClip = null;
      localObject = localElementsTransferable.getFirstObject();
      if ((localObject != null) && (localObject instanceof CellElementsClip))
        localCellElementsClip = (CellElementsClip)localObject;
      if (localCellElementsClip != null)
      {
        int j = this.oldCellRectangle.y - this.oldCellRectangle.height;
        while (j + this.oldCellRectangle.height > this.dragCellRectangle.y)
        {
          pasteElementsCopyToReport(localCellElementsClip, this.reportPane.getEditingReport(), this.oldCellRectangle.x, j, this.oldCellRectangle.x, Math.max(j, this.dragCellRectangle.y), this.oldCellRectangle.width, Math.min(this.oldCellRectangle.height, this.oldCellRectangle.height - this.dragCellRectangle.y - j));
          j -= this.oldCellRectangle.height;
        }
      }
      GridSelection localGridSelection2 = this.reportPane.getGridSelection();
      localGridSelection2.setType(0);
      localGridSelection2.setEditBounds(this.editRectangle.x, this.editRectangle.y, this.editRectangle.width, this.editRectangle.height);
      localGridSelection2.setEditBounds(this.dragCellRectangle.x, this.dragCellRectangle.y, this.dragCellRectangle.width, this.dragCellRectangle.height);
      this.isDataChanged = true;
    }
    else if (this.direction == 2)
    {
      if (this.dragCellRectangle.width == this.oldCellRectangle.width)
      {
        this.isNeedRepaint = true;
      }
      else if (this.dragCellRectangle.width > this.oldCellRectangle.width)
      {
        localElementsTransferable = GridUtils.caculateElementsTransferable(this.reportPane);
        localCellElementsClip = null;
        localObject = localElementsTransferable.getFirstObject();
        if ((localObject != null) && (localObject instanceof CellElementsClip))
          localCellElementsClip = (CellElementsClip)localObject;
        if (localCellElementsClip != null)
        {
          int k = this.oldCellRectangle.x + this.oldCellRectangle.width;
          while (k < this.dragCellRectangle.x + this.dragCellRectangle.width)
          {
            pasteElementsCopyToReport(localCellElementsClip, this.reportPane.getEditingReport(), k, this.oldCellRectangle.y, k, this.oldCellRectangle.y, Math.min(this.oldCellRectangle.width, this.dragCellRectangle.x + this.dragCellRectangle.width - k), this.oldCellRectangle.height);
            k += this.oldCellRectangle.width;
          }
        }
        GridSelection localGridSelection3 = this.reportPane.getGridSelection();
        localGridSelection3.setType(0);
        localGridSelection3.setEditBounds(this.editRectangle.x, this.editRectangle.y, this.editRectangle.width, this.editRectangle.height);
        localGridSelection3.setEditBounds(this.dragCellRectangle.x, this.dragCellRectangle.y, this.dragCellRectangle.width, this.dragCellRectangle.height);
        this.isDataChanged = true;
      }
    }
    else if (this.direction == 3)
    {
      localElementsTransferable = GridUtils.caculateElementsTransferable(this.reportPane);
      localCellElementsClip = null;
      localObject = localElementsTransferable.getFirstObject();
      if ((localObject != null) && (localObject instanceof CellElementsClip))
        localCellElementsClip = (CellElementsClip)localObject;
      if (localCellElementsClip != null)
      {
        int l = this.oldCellRectangle.x - this.oldCellRectangle.width;
        while (l + this.oldCellRectangle.width > this.dragCellRectangle.x)
        {
          pasteElementsCopyToReport(localCellElementsClip, this.reportPane.getEditingReport(), l, this.oldCellRectangle.y, Math.max(l, this.dragCellRectangle.x), this.oldCellRectangle.y, Math.min(this.oldCellRectangle.width, this.oldCellRectangle.width - this.dragCellRectangle.x - l), this.oldCellRectangle.height);
          l -= this.oldCellRectangle.width;
        }
      }
      GridSelection localGridSelection4 = this.reportPane.getGridSelection();
      localGridSelection4.setType(0);
      localGridSelection4.setEditBounds(this.editRectangle.x, this.editRectangle.y, this.editRectangle.width, this.editRectangle.height);
      localGridSelection4.setEditBounds(this.dragCellRectangle.x, this.dragCellRectangle.y, this.dragCellRectangle.width, this.dragCellRectangle.height);
      this.isDataChanged = true;
    }
    this.reportPane.fireGridSelectionChanged();
    if (this.isDataChanged)
      this.reportPane.repaint();
  }

  private void analyzeDirection()
  {
    if ((this.dragCellRectangle.x == this.oldCellRectangle.x) && (this.dragCellRectangle.width == this.oldCellRectangle.width))
      if (this.dragCellRectangle.y == this.oldCellRectangle.y)
        this.direction = 0;
      else if (this.dragCellRectangle.y < this.oldCellRectangle.y)
        this.direction = 1;
    else if ((this.dragCellRectangle.y == this.oldCellRectangle.y) && (this.dragCellRectangle.height == this.oldCellRectangle.height))
      if (this.dragCellRectangle.x == this.oldCellRectangle.x)
        this.direction = 2;
      else if (this.dragCellRectangle.x < this.oldCellRectangle.x)
        this.direction = 3;
  }

  private void defineRules()
  {
  }

  private Formula generateSimpleFormula(Formula paramFormula, int paramInt1, int paramInt2)
  {
    Formula localFormula;
    try
    {
      localFormula = (Formula)paramFormula.clone();
    }
    catch (CloneNotSupportedException localCloneNotSupportedException)
    {
      localFormula = new Formula();
      FRContext.getLogger().log(Level.WARNING, localCloneNotSupportedException.getMessage(), localCloneNotSupportedException);
    }
    String str1 = paramFormula.getContent();
    StringBuffer localStringBuffer = new StringBuffer();
    String str2 = "[a-z|A-Z]+[0-9]+";
    Pattern localPattern = Pattern.compile(str2);
    Matcher localMatcher = localPattern.matcher(str1);
    int i = 0;
    while (true)
    {
      int j;
      int k;
      int i4;
      int i6;
      while (true)
      {
        if (!(localMatcher.find()))
          break label274;
        j = localMatcher.start();
        k = localMatcher.end();
        int l = str1.charAt(j - 1);
        if (l != 36)
          break;
      }
      String str3 = str1.substring(j, k);
      ColumnRow localColumnRow1 = BaseUtils.convertCellStringToColumnRow(str3);
      int i1 = localColumnRow1.getColumn();
      int i2 = localColumnRow1.getRow();
      int i3 = i1;
      int i5 = i2;
      if (paramInt2 == 0)
      {
        i5 += paramInt1;
      }
      else if (paramInt2 == 2)
      {
        i3 += paramInt1;
      }
      else if (paramInt2 == 1)
      {
        i5 -= paramInt1;
        if (i5 < 0)
          i6 = 0;
      }
      else if (paramInt2 == 3)
      {
        i3 -= paramInt1;
        if (i3 < 0)
          i4 = 0;
      }
      ColumnRow localColumnRow2 = ColumnRow.valueOf(i4, i6);
      String str4 = BaseUtils.convertColumnRowToCellString(localColumnRow2);
      localStringBuffer.append(str1.substring(i, j));
      localStringBuffer.append(str4);
      i = k;
    }
    label274: localStringBuffer.append(str1.substring(i, str1.length()));
    localFormula.setContent(localStringBuffer.toString());
    return localFormula;
  }

  private void applyStyle(CellElement paramCellElement1, CellElement paramCellElement2)
  {
    if (this.isStyleSupported)
      paramCellElement1.setStyle(paramCellElement2.getStyle());
  }

  public static void pasteElementsCopyToReport(CellElementsClip paramCellElementsClip, Report paramReport, int paramInt1, int paramInt2, int paramInt3, int paramInt4, int paramInt5, int paramInt6)
  {
    int i = paramCellElementsClip.getCellElementCount();
    for (int j = 0; j < i; ++j)
    {
      CellElement localCellElement = paramCellElementsClip.getCellElement(j);
      localCellElement = localCellElement.deriveCellElement(paramInt1 + localCellElement.getColumn(), paramInt2 + localCellElement.getRow());
      if ((localCellElement.getColumn() < paramInt3 + paramInt5) && (localCellElement.getRow() < paramInt4 + paramInt6) && (localCellElement.getColumn() >= paramInt3))
      {
        if (localCellElement.getRow() < paramInt4)
          break label116:
        paramReport.addCellElement(localCellElement);
      }
    }
    label116:
  }
}